<?php
include_once("../../php/include_all.php");
session_start();
?>
<div class="row-fluid">
	<div class="span12">
		<h3 class="page-title">
            <b>Data Sub Kegiatan</b>
        </h3>
		       <a href="#modalwin" data-toggle="modal" class="btn btn-primary btn-tambah">Tambah Sub Kegiatan</a>
        <div id="modalwin" class="modal hide fade" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">

             <header class="modal-header">
			<a href="#" class="close geo-clear" data-dismiss="modal">x</a>
                <h3>Data Sub Kegiatan</h3>
            </header>
            <div class="modal-body">
              <form id="form_subkegiatan"  class="form-horizontal">
					<input type="hidden" name="act" value="subkegiatan_action">
					<input type="hidden" id="id_subkegiatan" name="id_subkegiatan" value="0">
					<div class="control-group">
                        <label class="control-label">Sub Kegiatan</label>
                        <div class="controls">
                            <select name="id_kegiatan" id="id_kegiatan">
								<option id="id_keg" value="0">Pilih salah satu</option>
								<?php
									$keg = get_datas("SELECT * from spm_kegiatan k,spm_indikator i,spm_jenis_pelayanan j where i.id_skpd=".$_SESSION['_id']." AND i.id_pelayanan=j.id_pelayanan AND i.id_indikator=k.id_indikator");
									foreach($keg as $keg){
								?>
									<option value=<?php echo $keg['id_kegiatan']; ?>><?php echo $keg['nama_kegiatan']; ?></option>
								<?php
								}
								?>
							</select>
                        </div>
					</div>
					<div class="control-group">
                        <label class="control-label">Kode Sub Kegiatan</label>
                        <div class="controls">
                            <input type="text" id="kode_subkegiatan" name="kode_subkegiatan" class="input-small" readonly />
                        </div>
					</div>
					<div class="control-group">
                        <label class="control-label">Nama Sub Kegiatan</label>
                        <div class="controls">
                            <input type="text" id="nama_subkegiatan" name="nama_subkegiatan" class="input-xlarge" />
                        </div>
					</div>
						<div class="control-group">
                        <label class="control-label">Satuan</label>
                        <div class="controls">
                            <input type="text" id="satuan" name="satuan" class="input-xlarge" />
                        </div>
					</div>
						<div class="control-group">
                        <label class="control-label">Rekening</label>
                        <div class="controls">
                              <select name="id_rekening" id="id_rekening">
								<option id="id_rek" value="0">Pilih salah satu</option>
								<?php
									$rek = get_datas("SELECT * FROM spm_rekening order by id_rekening");
									foreach($rek as $rek){
								?>
									<option value=<?php echo $rek['id_rekening']; ?>><?php echo $rek['nama_rekening']; ?></option>
								<?php
								}
								?>
							</select>
                        </div>
					</div>
                      <a class="btn btn-primary bt-simpan-subkegiatan"><div id="loader" style="margin-top:2px"></div>&nbsp;Save</a>
                                <span id="result"></span>
				</form>
            </div>
		</div>
	</div>
	</div>
	</br>
	<div class="row-fluid">
        <div class="span12">
			<div class="widget" style="">
                <div class="widget-title">
                    <h4><i class="icon-reorder"></i>Tabel Sub Kegiatan</h4>
                    <span class="tools">
                        <a href="javascript:;" class="icon-chevron-down"></a>
                    </span>
                </div>
				<div class="widget-body">
                    <table class="table table-striped table-bordered" id="sample_1">
                    <thead>
                        <tr>
                            <th class="hidden-phone" style="width:20px; text-align:center;">No</th>
                            <th style="text-align:center;">Kode Kegiatan</th>
                            <th style="text-align:center;">Nama Kegiatan</th>
                            <th style="text-align:center;">Kode Sub Kegiatan</th>
                            <th style="text-align:center;">Nama Sub Kegiatan</th>
							<th style="text-align:center;">Satuan</th>
							<th style="text-align:center;">Rekening</th>
							<th style="text-align:center;">Edit</th>
                        </tr>
                    </thead>
                    <tbody id="tb-subkegiatan">
					<?php 
					$no=1;
					$subkeg = get_datas("SELECT * FROM spm_rekening r,spm_subkegiatan sb, spm_indikator i,spm_kegiatan k where i.id_skpd=".$_SESSION['_id']." AND i.id_indikator=k.id_indikator AND sb.id_kegiatan=k.id_kegiatan AND sb.id_rekening=r.id_rekening");
					foreach ($subkeg as $subkeg){
					?>
						<tr>
							<td><?php echo $no ?></td>
							<td><?php echo $subkeg['kode_kegiatan'];?></td>
							<td><?php echo $subkeg['nama_kegiatan']?></td>
							<td><?php echo $subkeg['kode_subkegiatan']?></td>
							<td><?php echo $subkeg['nama_subkegiatan']?></td>
							<td><?php echo $subkeg['satuan']?></td>
							<td><?php echo $subkeg['nama_rekening']?></td>
							<td style="width:200px; text-align:center;">
							<a class="btn btn-danger bt-delete-subkegiatan" name="<?php echo $subkeg['id_subkegiatan']; ?>">Delete</a>
                                <a  href="#modalwin" data-toggle="modal"  class="btn btn-info bt-edit-subkegiatan" name="<?php echo $subkeg['id_subkegiatan']; ?>">Edit</a>
							</td>
						</tr>
						<?php 
						$no++;
						}
						?>
					</tbody>
				</div>
			</div>
		</div>
	</div>
</div>
<script type="text/javascript" src="../assets/data-tables/jquery.dataTables.js"></script>
<script type="text/javascript" src="../assets/data-tables/DT_bootstrap.js"></script>
<script>
    jQuery(document).ready(function() {       
         // initiate layout and plugins
        App.init();
    });
</script>

<script>
var urls = 'master_subkegiatan/subkegiatan_action.php';

$('#id_kegiatan').change(function(){
    source ="master_subkegiatan/id.php?ids="+this.value;
    var coba=$.get(source);
    coba.done(function(res){          
        document.getElementById('kode_subkegiatan').value=res;	
    })
});
	
$('.bt-simpan-subkegiatan').click(function(){
	var btn  = $(this);
	var load = $('#loader');
	var rslt = $('#result');
	load.addClass('spinner pull-left');
	btn.removeClass('btn-primary').addClass('btn-info');
	
	var form = $('#form_subkegiatan');
	var data = form.serializeArray();
	var post = $.post(urls,data);
	post.done(function(res){
		var result = res.split('__');
		//   0      1     2
		//success__id__(isi tbody)tr
		if(result.length==3){
			if(result[0]=='success'){
				rslt.html('<font color="green">tersimpan...</font>');
				setTimeout(function(){
					rslt.html('');
				},1500);
				var tbody = $('#tb-subkegiatan');
				tbody.html(result[2]);
				document.getElementById('form_subkegiatan').reset();
				 //$('#thn_angg').val(' ');
				init();
			}else{
				rslt.html('<font color="red">'+res+'</font>');
			}
		}else{
			rslt.html('<font color="red">'+res+'</font>');
		}
		load.removeClass();
		btn.removeClass('btn-info').addClass('btn-primary');
	});
});
	
function init(){
$('.bt-delete-subkegiatan').click(function(){
			var btn = $(this);
			var pid = this.name;
			var load = $(this);
			bootbox.confirm("Anda akan menghapus data ini?", function(result) {
                if(result==true){
                    if(pid!=0){
                        var post = $.post(urls,{act:'del_subkegiatan',id_subkegiatan:pid});
                        post.done(function(res){
                            var hasil = res.split('__');
						if(hasil.length==2){
							if(hasil[0] == 'success'){
								var tbody = $('#tb-subkegiatan');
								tbody.html(hasil[1]);
								init();
									}
								}
                        });
                        load.html('Delete');
                    }
                }else{
                    load.html('Delete');
                }
            });
		});
	
	$('.bt-edit-subkegiatan').click(function(){
		var id_subkegiatan = this.name;
		var form = $('#form_subkegiatan');
		var post = $.post(urls,{act:'edit_subkegiatan',id_subkegiatan:id_subkegiatan});
		post.done(function(res){
			var value = res.split('__');
			$('#id_subkegiatan').val(value[0]);
                form.find('select[name="id_kegiatan"]').val(value[1]); 
				form.find('input[name=kode_subkegiatan]').val(value[2]);
                form.find('input[name="nama_subkegiatan"]').val(value[3]);
                form.find('input[name="satuan"]').val(value[4]);
                form.find('select[name="id_rekening"]').val(value[5]);
		});
	});
}

$('.geo-clear').click(function(){
	$('#id_subkegiatan').val(0);
	document.getElementById('form_subkegiatan').reset();
});
init();
</script>